CC       = gcc
CFLAGS   = -Wall  -ggdb3 -Iinclude # debug: put -ggdb instead of -O3
LDFLAGS  = -L/usr/lib -L/usr/local/lib -L./lib 
LIBS     = -loctopus_common 
DEFS     = -D_DEBUG_ -D_MAIN_H_ 
GNUDEF   = -D_GNU_SOURCE
LPTHREAD = -lpthread
LIBNAME  = lib/liboctopus_common.so
OBJECTS  = queue.o mqueue.o hash.o list.o conf_helper.o digest.o \
	   string_utils.o log.o appender.o memory.o timers.o log_pthread.o
TESTFILS = t_queue t_hash t_list \
	   t_mqueue t_string_utils t_conf_helper \
	   t_timers t_log
VPATH    = src:include:test

all:$(LIBNAME)

$(LIBNAME):$(OBJECTS)
	$(CC) -shared -fPCI $(LDFLAGS) $(LPTHREAD) $^ -o $(LIBNAME)
	-rm -rf *.o

$(OBJECTS):%.o:%.c
	$(CC) -c $(CFLAGS) ${DEFS} ${GNUDEF} $^ -o $@

test_all: $(TESTFILS) 
	cp $(LIBNAME) bin/

t_hash:t_hash.c $(LIBNAME) 
	$(CC) $(CFLAGS) test/t_hash.c -o bin/t_hash -loctopus_common $(LDFLAGS)

t_queue: t_queue.c  $(LIBNAME) 
	$(CC) $(CFLAGS) test/t_queue.c -o bin/t_queue -loctopus_common $(LDFLAGS)

t_list: t_list.c  $(LIBNAME)
	$(CC) $(CFLAGS) test/t_list.c -o bin/t_list -loctopus_common $(LDFLAGS)

t_mqueue: t_mqueue.c $(LIBNAME)
	$(CC) $(CFLAGS) test/t_mqueue.c -o bin/t_mqueue -loctopus_common $(LPTHREAD) $(LDFLAGS)

t_string_utils: t_string_utils.c $(LIBNAME)
	$(CC) $(CFLAGS) test/t_string_utils.c -o bin/t_string_utils -loctopus_common $(LDFLAGS)

t_conf_helper: t_conf_helper.c $(LIBNAME)
	$(CC) $(CFLAGS) test/t_conf_helper.c -o bin/t_conf_helper -loctopus_common $(LDFLAGS)

t_log:t_log.c $(LIBNAME)
	$(CC) $(CFLAGS) test/t_log.c -o bin/t_log -loctopus_common $(LPTHREAD) $(LDFLAGS) $(DEFS)

t_timers: t_timers.c $(LIBNAME)
	$(CC) $(CFLAGS) test/t_timers.c -o bin/t_timers -loctopus_common $(LDFLAGS)

install:$(LIBNAME)
	-sudo cp $(LIBNAME) /usr/lib/

updatetags: 
	ctags -R --c-kinds=+p --fields=+iatS --extra=+q
	cscope -b

clean:
	-find . \( -name '*.o' -o -name '*.so' -o -name '*.bak' -o \
	        -name '*.swp' -o -name '*.out' -o -name '*.gch' -o -name '*.*~' -o \
	       -name 'core.*' -o -name '*.gch' -o -name '*.files' \) -type f -execdir rm -f '{}' \; 
	-cd bin/ && rm -rf $(TESTFILS)

.PHONY: clean
